package com.dgut.lotterySystem.service.activityStatus.operator;

import com.dgut.lotterySystem.dao.dataObject.ActivityPrizeDO;
import com.dgut.lotterySystem.dao.mapper.ActivityPrizeMapper;
import com.dgut.lotterySystem.service.dto.ConvertActivityStatusDTO;
import com.dgut.lotterySystem.service.enums.ActivityPrizeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class PrizeOperator extends AbstractActivityOperator{
    @Autowired
    private ActivityPrizeMapper activityPrizeMapper;
    @Override
    public Integer sequence() {
        return 1;
    }

    @Override
    public Boolean needConvert(ConvertActivityStatusDTO convertActivityStatusDTO) {
        //需要判断是否为Init
        Long activityId=convertActivityStatusDTO.getActivityId();
        Long prizeId=convertActivityStatusDTO.getPrizeId();
        ActivityPrizeEnum activityPrizeEnum=convertActivityStatusDTO.getTargetActivityPrizeStatus();
        if(activityId==null
                ||prizeId==null
                ||activityPrizeEnum==null){
            return false;
        }
        ActivityPrizeDO activityPrizeDO=activityPrizeMapper.selectByActivityPrizeId(activityId,prizeId);
        if(activityPrizeDO==null){
            return false;
        }

        if(activityPrizeDO.getStatus()
                .equalsIgnoreCase(activityPrizeEnum.name())){
            return false;
        }

        return true;
    }

    @Override
    public Boolean convert(ConvertActivityStatusDTO convertActivityStatusDTO) {
        Long activityId=convertActivityStatusDTO.getActivityId();
        Long prizeId=convertActivityStatusDTO.getPrizeId();
        ActivityPrizeEnum activityPrizeEnum=convertActivityStatusDTO.getTargetActivityPrizeStatus();
        try{
            activityPrizeMapper.updateStatus(activityId,prizeId,activityPrizeEnum.name());
            return true;
        }catch (Exception e){
            return false;
        }
    }


}
